package com.blog.cloud.service.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blog.cloud.api.dto.BlogDto;
import com.blog.cloud.api.entity.Blog;
import com.blog.cloud.core.utils.R;
import com.blog.cloud.service.service.BlogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 博客表(Blog)表控制层
 *
 * @author 龙茶清欢
 * @since 2023-02-27 19:09:33
 */
@RestController
@RequestMapping("/blog")
@RequiredArgsConstructor
@Tag(name = "博客表管理", description = "博客表管理")
public class BlogController {

    private final BlogService blogService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @PostMapping("/findBlogByPage")
    @Operation(description = "通过分页查询")
    public R<Object> findBlogByPage(@RequestBody BlogDto blogDto) {
        Page<Blog> blogPage = blogService.findBlogByPage(blogDto);

        return R.ok(blogPage);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("/findBlogByBlogId/{blogId}")
    @Operation(description = "通过ID查询")
    public R<Object> findBlogByBlogId(@PathVariable Long blogId) {
        return R.ok(blogService.getById(blogId));
    }

    /**
     * 新增数据
     *
     * @param blog 实体对象
     * @return 新增结果
     */
    @PostMapping("/insertBlog")
    @Operation(description = "新增")
    public R<Object> insert(@RequestBody @Valid Blog blog) {
        return R.ok(blogService.save(blog));
    }

    /**
     * 修改数据
     *
     * @param blog 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateBlog")
    @Operation(description = "更新")
    public R<Object> update(@RequestBody Blog blog) {
        return R.ok(blogService.updateById(blog));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteBlog")
    @Operation(description = "删除")
    public R<Object> delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(blogService.removeByIds(idList));
    }

}

